package com.example.expandtabview;

import com.example.view.ExpandTabView;
import com.example.view.ViewMiddle;
import com.example.view.ViewRight;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
  //dev分支测试
  //lyy分支测试
  //dev modify
  private ExpandTabView expandTabView;
  private ArrayList<View> mViewArray = new ArrayList<View>();
  private ViewMiddle viewMiddleOne;
  private ViewMiddle viewMiddle;
  private ViewRight viewRight;

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_main);
    this.initView();
    this.initVaule();
    this.initListener();

  }

  private void initView() {

    this.expandTabView = (ExpandTabView) this.findViewById(R.id.expandtab_view);
    this.viewMiddleOne = new ViewMiddle(this);
    this.viewMiddle = new ViewMiddle(this);
    this.viewRight = new ViewRight(this);

  }

  private void initVaule() {
    this.mViewArray.add(this.viewMiddleOne);
	this.mViewArray.add(this.viewMiddle);
    this.mViewArray.add(this.viewRight);
    ArrayList<String> mTextArray = new ArrayList<String>();
    mTextArray.add("hhh");
	mTextArray.add("ddd");
	mTextArray.add("ccc");
    this.expandTabView.setValue(mTextArray, this.mViewArray);
    this.expandTabView.setTitle(this.viewMiddleOne.getShowText(), 0);
    this.expandTabView.setTitle(this.viewMiddle.getShowText(), 1);
    this.expandTabView.setTitle(this.viewRight.getShowText(), 2);

  }

  private void initListener() {
	  
    this.viewMiddleOne.setOnSelectListener(new ViewMiddle.OnSelectListener() {

        @Override
        public void getValue(String showText) {

          MainActivity.this.onRefresh(MainActivity.this.viewMiddle,showText);

        }
      });
    
    this.viewMiddle.setOnSelectListener(new ViewMiddle.OnSelectListener() {

      @Override
      public void getValue(String showText) {

        MainActivity.this.onRefresh(MainActivity.this.viewMiddle,showText);

      }
    });

    this.viewRight.setOnSelectListener(new ViewRight.OnSelectListener() {

      @Override
      public void getValue(String distance, String showText) {
        MainActivity.this.onRefresh(MainActivity.this.viewRight, showText);
      }
    });

  }

  private void onRefresh(View view, String showText) {

    this.expandTabView.onPressBack();
    int position = this.getPositon(view);
    if (position >= 0 && !this.expandTabView.getTitle(position).equals(showText)) {
      this.expandTabView.setTitle(showText, position);
    }
    Toast.makeText(MainActivity.this, showText, Toast.LENGTH_SHORT).show();

  }

  private int getPositon(View tView) {
    for (int i = 0; i < this.mViewArray.size(); i++) {
      if (this.mViewArray.get(i) == tView) {
        return i;
      }
    }
    return -1;
  }

  @Override
  public void onBackPressed() {

    if (!this.expandTabView.onPressBack()) {
      this.finish();
    }

  }

}
